// FileUtil.cpp: implementation of the CFileUtil class.
//
#include "FileUtil.h"
#include <assert.h>
#include <sys/utime.h>
#include <time.h>
#include <tchar.h>
#include <ShlObj.h>
#include "io.h"
#pragma comment(lib, "shell32.lib")



#pragma warning(disable:4996)



BOOL CFileUtil::ChangeFileExtName(LPTSTR szFileName, LPCTSTR szExtName, UINT uMaxLen)
{
	return ChangeFileExtName(szFileName, szExtName, szFileName, uMaxLen);
}

BOOL CFileUtil::ChangeFileExtName(LPCTSTR szOldName, LPCTSTR szExtName, LPTSTR szFileName, UINT uMaxLen)
{
	assert( szOldName != NULL && szExtName != NULL && szFileName != NULL);

	TCHAR szDrive[_MAX_DRIVE] = _T("");
	TCHAR szDir[_MAX_DIR] = _T("");
	TCHAR szFName[_MAX_FNAME] = _T("");
	TCHAR szExt[_MAX_EXT] = _T("");

	_tsplitpath(szOldName, szDrive, szDir, szFName, szExt);
	if( _tcslen(szDrive) + _tcslen(szDir) + _tcslen(szFName) + _tcslen(szExtName) >= uMaxLen )
		return FALSE;
	_tmakepath(szFileName, szDrive, szDir, szFName, szExtName);
	return TRUE;
}


BOOL CFileUtil::ChangeFileBaseName(LPTSTR szFileName, LPCTSTR szBaseName, UINT uMaxLen)
{
	return ChangeFileBaseName(szFileName, szBaseName, szFileName, uMaxLen);
}

BOOL CFileUtil::ChangeFileBaseName(LPCTSTR szOldName, LPCTSTR szBaseName, LPTSTR szFileName, UINT uMaxLen)
{
	TCHAR szDirName[_MAX_PATH] = _T("");
	GetFileDirName (szOldName, szDirName, _MAX_PATH);

	if ( !CheckFullNameLegal(szBaseName))
	{
		int len = _tcslen(szDirName);
		if( len + 1 >= (int)uMaxLen )
			return FALSE;

		_tcscpy(szFileName, szDirName);
		if( szFileName[len-1] != _T('\\') &&
			szFileName[len-1] != _T('/'))
		{
			_tcscat(szFileName, _T("\\"));
			len ++;
		}

		if( len + _tcslen(szBaseName) >= uMaxLen )
			return FALSE;

		_tcscat(szFileName, szBaseName);
	}
	else
	{
		_tcsncpy(szFileName, szBaseName, uMaxLen);
	}

	return TRUE;
}


BOOL CFileUtil::CheckFullNameLegal (LPCTSTR szName)
{
	int len = _tcslen(szName);
	int nBeginPos = 0;

	bool bSlash = TRUE;

	if(len <= 2) 
	{
		return FALSE;
	}

	if( szName[0] == _T(' ') || szName[len-1] == _T(' ') ) return FALSE;

	if( (szName[1] == _T(':') && szName[2] == _T('\\') ))
	{
		nBeginPos = 3;
	}
	else 
	{
		if((szName[0] == _T('\\') && szName[1] == _T('\\')))
		{
			nBeginPos = 2;
		}
		else		
		{
			return FALSE;
		}

		for(int i=nBeginPos; i<len; i++) 
		{
			if(szName[i] == _T('\\') || szName[i] == _T('/'))
			{
				if( bSlash )
					return FALSE;
				bSlash = TRUE;
				continue;
			}

			if( szName[i] != _T(' ') )
				bSlash = FALSE;

			if( _tcschr(_T("<>*?|\"\'\t"), szName[i])) 
				return FALSE;
		}	
	}

	return TRUE;
}

BOOL CFileUtil::CreateFile(LPCTSTR pszFilePath)
{
	if(NULL == pszFilePath)
		return FALSE;

	if(IsFileOrDirExist(pszFilePath))
		return FALSE;

	FILE* pFile = _tfopen(pszFilePath, _T("wb"));
	if (NULL == pFile)
		return FALSE;

	fclose(pFile);

	return TRUE;
}

BOOL CFileUtil::GetFileTime(LPCTSTR pszFilePath, SYSTEMTIME& sysTime)
{
	if (IsPathValid(pszFilePath))
	{
		HANDLE hDir = ::CreateFile(pszFilePath, GENERIC_READ, FILE_SHARE_READ | 
			FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);

		FILETIME lpCreationTime;
		FILETIME lpLastAccessTime;
		FILETIME lpLastWriteTime;
		if (::GetFileTime(hDir, &lpCreationTime, &lpLastAccessTime, &lpLastWriteTime))  
		{
			FILETIME ftime;
			FileTimeToLocalFileTime(&lpCreationTime, &ftime);
			FileTimeToSystemTime(&ftime, &sysTime) ;
			CloseHandle(hDir);	
			return TRUE;
		}
		CloseHandle(hDir);
	}
	return FALSE;
}

BOOL CFileUtil::GetAppPath(TCHAR* szBuffer, UINT nMaxSize)
{
	if(NULL == szBuffer)
		return FALSE;

	TCHAR szAppPath[MAX_PATH] = _T("");
	::GetModuleFileName(NULL, szAppPath, MAX_PATH);
	_tcsrchr(szAppPath,_T('\\'))[1] = _T('\0');

	int nLen = (_tcslen(szAppPath) < nMaxSize)? _tcslen(szAppPath):nMaxSize;	
	_tcsncpy(szBuffer, szAppPath, nLen);

	return TRUE;
}

void CFileUtil::GetUserPath(TCHAR *pFileName, const TCHAR*pName)
{
	TCHAR path[MAX_PATH] = {0};
	SHGetSpecialFolderPath(NULL,path, CSIDL_PERSONAL,0);

	_stprintf_s(pFileName, MAX_PATH, _T("%s\\%s"), path, pName);

	TCHAR szBuffer[MAX_PATH] = {0};
	GetFileDirName(pFileName, szBuffer, MAX_PATH);
	CreateDirectory(szBuffer);
}

BOOL CFileUtil::GetCurUserDocFilePath(LPTSTR szFullFileName, UINT nMaxLen, LPTSTR szParam)
{
	
	TCHAR szAppdataFileName[_MAX_PATH] = _T("\0");
	GetEnvironmentVariable(_T("UserProfile"), szAppdataFileName, _MAX_PATH);

	TCHAR szBuffer[MAX_PATH] = {0};
	_tcscpy(szBuffer, szAppdataFileName);
	_tcscat(szBuffer, _T("\\My Documents"));

	//check work directory
	TCHAR strAppdataFileName[MAX_PATH];
	_stprintf(strAppdataFileName, _T("%s%s"),szBuffer,szParam);
	if (!IsFileOrDirExist(strAppdataFileName))
	{
		BOOL bCreated = CreateDirectory(strAppdataFileName);
		if(!bCreated)
		{
			DWORD dw = GetLastError();
			return FALSE;
		}
	}

	_tcscpy(szFullFileName, szAppdataFileName);
	_tcscat(szFullFileName, szParam);
	CreateDirectory(szFullFileName);

	return TRUE;
}

BOOL CFileUtil::GetWindowsPath(LPTSTR szFullFileName, UINT nMaxLen, LPTSTR szParam)
{
	TCHAR szBuffer[MAX_PATH] = {0};
	GetWindowsDirectory(szBuffer, MAX_PATH);

	TCHAR szSlashBuffer[MAX_PATH] = {0};
	SlashDir(szBuffer,szSlashBuffer,MAX_PATH);

	_tcscpy(szFullFileName, szSlashBuffer);
	_tcscat(szFullFileName, szParam);

	CreateDirectory(szFullFileName);
	memset(szSlashBuffer,0,MAX_PATH);
	SlashDir(szFullFileName,szSlashBuffer,MAX_PATH);
	_tcscpy(szFullFileName,szSlashBuffer);

	return TRUE;
}

int CFileUtil::GetFileList(LPCTSTR szSearchDir,
						   BOOL bGetFileByExtName,
						   LPCTSTR lpFileExtName,
						   BOOL bGetFullPath,
						   BOOL bRecDir,
						   vector<tstring>& fileList)
{
	if(NULL == szSearchDir) 
		return 0;

	TCHAR szDirName[MAX_PATH] = {0};
	_tcscpy(szDirName, szSearchDir);

	int nLen = _tcslen(szSearchDir);
	if (_T('\\') != szDirName[nLen - 1] &&
		_T('/') != szDirName[nLen - 1])
	{
		_tcscat(szDirName, _T("\\"));
	}

	TCHAR szParentDir[MAX_PATH] = {0};
	_tcscpy(szParentDir, szDirName);

	_tcscat(szDirName, _T("*.*"));

	WIN32_FIND_DATA stFindData;
	ZeroMemory(&stFindData, sizeof(stFindData));

	HANDLE hFile = FindFirstFile(szDirName, &stFindData);
	if (NULL == hFile || INVALID_HANDLE_VALUE == hFile)
	{
		return 0;
	}
	
	BOOL bFind = TRUE;
	while (bFind)
	{
		if (0 == _tcscmp(_T("."), stFindData.cFileName) ||
			0 == _tcscmp(_T(".."), stFindData.cFileName))
		{
			bFind = FindNextFile(hFile, &stFindData);
			continue;
		}

		if (stFindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
		{
			if (bRecDir)
			{
				TCHAR szCurrentDir[MAX_PATH] = {0};
				_tcscpy(szCurrentDir, szParentDir);
				_tcscat(szCurrentDir, stFindData.cFileName);
				GetFileList(szCurrentDir, bGetFileByExtName, lpFileExtName, bGetFullPath, bRecDir, fileList);			
			}
		}
		else
		{
			TCHAR szFileName[MAX_PATH] = {0};
			if(bGetFullPath)
				_tcscpy(szFileName, szParentDir);
			_tcscat(szFileName, stFindData.cFileName);

			if (bGetFileByExtName)
			{
				TCHAR szExtName[MAX_PATH] = {0};
				GetFileExtName(szFileName, szExtName, MAX_PATH);
				if (0 == _tcsicmp(szExtName, lpFileExtName))
				{
					tstring strFileName = szFileName;
					fileList.push_back(strFileName);
				}
			}
			else
			{
				tstring strFileName = szFileName;
				fileList.push_back(strFileName);
			}
		}
		
		bFind = FindNextFile(hFile, &stFindData);
	}

	FindClose(hFile);
	hFile = NULL;

	return fileList.size();
}

BOOL CFileUtil::GetFileExtName(LPCTSTR szFileName, LPTSTR lpExtName, int nLen)
{
	TCHAR szDrive[_MAX_DRIVE] = _T("");
	TCHAR szDir[_MAX_DIR] = _T("");
	TCHAR szFName[_MAX_FNAME] = _T("");
	TCHAR szExt[_MAX_EXT] = _T("");

	_tsplitpath(szFileName, szDrive, szDir, szFName, szExt);
	int  actuLen = _tcslen(szExt);
	if( nLen-1 > actuLen )
	{
		_tcscpy(lpExtName,&(szExt[1]));
		return TRUE;
	}
	else
	{
		_tcsncpy(lpExtName,&(szExt[1]), nLen-1);
		lpExtName[nLen-1] = _T('\0');
		return FALSE;
	}
}

BOOL CFileUtil::GetFileName(LPCTSTR pszFilePath, TCHAR* szBuffer, UINT nMaxSize)
{
	if (NULL != pszFilePath && NULL != szBuffer)
	{
		TCHAR szDriver[MAX_PATH] = {0};
		TCHAR szParent[MAX_PATH] = {0};
		TCHAR szFileTit[MAX_PATH] = {0};
		TCHAR szFileExt[MAX_PATH] = {0};

		_tsplitpath(pszFilePath,szDriver,szParent,szFileTit,szFileExt);

		TCHAR pszName[MAX_PATH] = {0};
		_tcscpy(pszName,szFileTit);
		_tcscat(pszName,szFileExt);

		UINT nLen = (_tcslen(pszName) > nMaxSize) ? nMaxSize: _tcslen(pszName);
		_tcsncpy(szBuffer, pszName, nLen);
		return TRUE;
	}
	return FALSE;
}

BOOL CFileUtil::GetFileTitleEx(LPCTSTR szFileName, TCHAR* szBuffer, UINT nMaxSize)
{
	BOOL bRet = TRUE;

	if (NULL == szFileName)
		return FALSE;

	TCHAR szDriver[MAX_PATH] = {0};
	TCHAR szParent[MAX_PATH] = {0};
	TCHAR szFileTit[MAX_PATH] = {0};
	TCHAR szFileExt[MAX_PATH] = {0};

	_tsplitpath(szFileName,szDriver,szParent,szFileTit,szFileExt);

	TCHAR pExt[MAX_PATH] = {0};
	_tcscpy(pExt,szFileTit);
	_tcscat(pExt,szFileExt);

	if (NULL == pExt)
		return FALSE;

	LPTSTR pEnd = _tcsrchr(pExt, _T('.'));

	_tcsncpy(szBuffer, pExt, pEnd - pExt);

	return bRet;
}

BOOL CFileUtil::SlashDir(LPCTSTR lpDirName, TCHAR* szBuffer, UINT nMaxLen)
{
	if(NULL == lpDirName)
		return FALSE;

	unsigned int nLen = _tcslen(lpDirName);
	if (nLen >= nMaxLen)
	{
		return FALSE;
	}

	ZeroMemory(szBuffer, nMaxLen* sizeof(TCHAR));
	_tcscpy(szBuffer, lpDirName);
	
	if (_T('\\') != szBuffer[nLen - 1] &&
		_T('/') != szBuffer[nLen - 1])
	{
		if(_tcsrchr(szBuffer, _T('\\')) != NULL)
			_tcscat(szBuffer, _T("\\"));
		else
			_tcscat(szBuffer, _T("/"));
	}

	return TRUE;
}

BOOL CFileUtil::IsPathValid(LPCTSTR lpszPath)
{
	if (NULL == lpszPath || !IsFileOrDirExist(lpszPath))
		return FALSE;
	return TRUE;
}


BOOL CFileUtil::GetFileDirName (LPCTSTR szFullFileName, TCHAR* szDirBuffer, UINT nMaxSize)
{
	assert( szFullFileName != NULL);
	if (NULL == szFullFileName)
		return FALSE;

	TCHAR szFullPath[_MAX_PATH] = _T("");
	_tcscpy(szFullPath, szFullFileName);
	int nFullLen = _tcslen(szFullPath);
	if (szFullPath[nFullLen - 1] == _T('\\'))
		szFullPath[nFullLen - 1] = _T('\0');

	TCHAR szDirName[_MAX_PATH] = _T("");
	TCHAR szDrive[_MAX_DRIVE] = _T("");
	TCHAR szDir[_MAX_DIR] = _T("");
	TCHAR szFName[_MAX_FNAME] = _T("");
	TCHAR szExt[_MAX_EXT] = _T("");

	_tsplitpath(szFullPath, szDrive, szDir, szFName, szExt);
	assert( _tcslen(szDrive) + _tcslen(szDir) <= _MAX_PATH ); 

	_tcscpy(szDirName, szDrive);
	_tcscat(szDirName, szDir);

	int len = _tcslen(szDirName);
	if ( szDirName[len-1] == _T('\\') || szDirName[len-1] == _T('/') ) 
		szDirName[len-1] = _T('\0');

	int nLen = (_tcslen(szDirName) < nMaxSize)? _tcslen(szDirName): nMaxSize ;
	_tcsncpy(szDirBuffer, szDirName, nLen);

	return TRUE;
}

BOOL CFileUtil::CreateDirectory(LPCTSTR pszFileName)
{
	if (IsPathValid(pszFileName))
		return TRUE;

	BOOL bRet = FALSE;
	if (_tcslen(pszFileName) >= 2)  
	{
		TCHAR szDirName[MAX_PATH] = {0};
		GetFileDirName(pszFileName, szDirName, MAX_PATH);
		if (CreateDirectory(szDirName))
		{
			if (::CreateDirectory(pszFileName, NULL))
			{
				bRet = SetFileAttributes(pszFileName, FILE_ATTRIBUTE_NORMAL);
			}
		}
	}

	return bRet;
}

BOOL CFileUtil::IsWriteAble(LPCTSTR pszPath, BOOL bDirectory)
{
	if (bDirectory)
	{
		TCHAR szDir[MAX_PATH] = {0};
		SlashDir(pszPath, szDir, MAX_PATH);
		_tcscat(szDir, _T("TestWritable")); 

		BOOL bRet = CreateDirectory(szDir);
		DeleteDir(szDir);
		return bRet;
	}
	
	return !(GetFileAttributes(pszPath) & FILE_ATTRIBUTE_READONLY);
}

BOOL CFileUtil::DeleteDir(LPCTSTR lpDirName)
{
	TCHAR szParentName[MAX_PATH] = {0};
	TCHAR szDirName[MAX_PATH] = {0};
	SlashDir(lpDirName, szDirName, MAX_PATH);
	_tcscpy(szParentName, szDirName);
	_tcscat(szDirName, _T("*.*"));

	WIN32_FIND_DATA data;
	ZeroMemory(&data, sizeof(data));

	HANDLE hFind = FindFirstFile(szDirName, &data);
	if (hFind != INVALID_HANDLE_VALUE)
	{
		do 
		{
			if (((data.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) != 0) ||
				((data.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) != 0))
				continue;
			if ((lstrcmp(data.cFileName, _T(".")) == 0) || 
				(lstrcmp(data.cFileName, _T("..")) == 0))
				continue;

			TCHAR szFileName[MAX_PATH] = {0};
			_tcscpy(szFileName, szParentName);
			_tcscat(szFileName, data.cFileName);

			if ((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
				DeleteDir(szFileName);
			else 
				::DeleteFile(szFileName);
		} 
		while(FindNextFile(hFind, &data));
	} 

	FindClose(hFind);
	hFind = NULL;

	SetFileAttributes(lpDirName, FILE_ATTRIBUTE_NORMAL);
	return RemoveDirectory(lpDirName);
}

BOOL CFileUtil::DeleteOneFile(LPCTSTR szFileName)
{
	return ::DeleteFile(szFileName);
}

BOOL CFileUtil::IsFileOrDirExist(LPCTSTR lpName)
{
	if (!lpName)
		return FALSE;

	return (-1 != _taccess(lpName,0));
}

BOOL CFileUtil::Rename(LPCTSTR lpOldFileOrPath, LPCTSTR lpNewFileOrPath)
{
	return (_trename(lpOldFileOrPath, lpNewFileOrPath) == 0);
}

BOOL CFileUtil::CopyFile(LPCTSTR lpSourceFile, LPCTSTR lpDesFile, BOOL bFailIfExists)
{
	return ::CopyFile(lpSourceFile, lpDesFile, bFailIfExists);
}

BOOL CFileUtil::CopyDirectory(LPCTSTR lpSourceDir, LPCTSTR lpDesDir, BOOL bFailIfExists)
{
	BOOL bret = TRUE;
	vector<tstring> fileList;
	GetFileList(lpSourceDir, FALSE, NULL, TRUE, TRUE, fileList);

	TCHAR szDestDir[MAX_PATH] = {0};
	SlashDir(lpDesDir, szDestDir, MAX_PATH);
	for (size_t nIndex = 0; nIndex < fileList.size(); nIndex ++)
	{
		tstring strFileName = fileList[nIndex];
		TCHAR szDestFile[MAX_PATH] = {0};

		TCHAR szFileName[MAX_PATH] = {0};
		GetFileName(strFileName.c_str(), szFileName, MAX_PATH);

		_tcscpy(szDestFile, szDestDir);
		_tcscat(szDestFile, szFileName);

		bret = CopyFile(strFileName.c_str(), szDestFile, bFailIfExists);
		if (!bret)
			break;
	}

	return bret;
}

BOOL CFileUtil::GetTempPath(LPCTSTR lpParam, TCHAR* szBuffer, UINT nMaxSize)
{
	TCHAR szTempPath[MAX_PATH] = {0};
	::GetTempPath(MAX_PATH, szTempPath);

	TCHAR szDirName[MAX_PATH] = {0};
	SlashDir(szTempPath, szDirName, MAX_PATH);

	LPCTSTR pTemp = NULL;
	pTemp = _tcschr(lpParam, _T('\\'));
	if (NULL == pTemp)
		pTemp = _tcschr(lpParam, _T('/'));

	if(NULL == pTemp)
		_tcscat(szDirName, lpParam);
	else
		_tcscat(szDirName, pTemp + 1);

	BOOL bRet = CreateDirectory(szDirName);

	UINT nLen = (_tcslen(szDirName) < nMaxSize) ? _tcslen(szDirName) : nMaxSize;
	_tcscpy(szBuffer, szDirName);

	return bRet;
}


BOOL CFileUtil::GetFileModifyTime(LPCTSTR pszFilePath, SYSTEMTIME& sysModifyTime)
{
	if (IsPathValid(pszFilePath))
	{
		HANDLE hDir = ::CreateFile(pszFilePath, GENERIC_READ, FILE_SHARE_READ | 
			FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);

		FILETIME lpCreationTime;
		FILETIME lpLastAccessTime;
		FILETIME lpLastWriteTime;
		if (::GetFileTime(hDir, &lpCreationTime, &lpLastAccessTime, &lpLastWriteTime))  
		{
			FILETIME ftime;
			FileTimeToLocalFileTime(&lpLastWriteTime, &ftime);
			FileTimeToSystemTime(&ftime, &sysModifyTime) ;
			CloseHandle(hDir);	
			return TRUE;
		}
		CloseHandle(hDir);
	}
	return FALSE;
}

LONG CFileUtil::GetFileSize(LPCTSTR szPath, DWORD* pdwHigh)
{
	HANDLE hFile = ::CreateFile(szPath, 
								GENERIC_READ, 
								FILE_SHARE_READ|FILE_SHARE_WRITE,
								NULL, OPEN_EXISTING, 
								FILE_ATTRIBUTE_NORMAL, 
								NULL);
	if (hFile == INVALID_HANDLE_VALUE || NULL == hFile) 
		return -1;

	LONG lFileSize = ::GetFileSize(hFile, pdwHigh);
	CloseHandle(hFile);
	hFile = NULL;

	return lFileSize;
}

BOOL CFileUtil::GetFullFileName (LPCTSTR szDirName, LPCTSTR szBaseName, LPTSTR szFullFileName, UINT uMaxLen)
{
	if(szDirName == NULL || szFullFileName == NULL && uMaxLen <= 0)
		return FALSE;

	TCHAR szTemp[MAX_PATH] = {0};
	_tcscpy(szTemp, szDirName);
	if(_T('\\') != szBaseName[0] &&
	   _T('/')  != szBaseName[0] && 
	   _T('\\') != szDirName[_tcslen(szDirName) - 1]&&
	   _T('/') != szDirName[_tcslen(szDirName) - 1])
	   SlashDir(szDirName, szTemp, MAX_PATH);

	_tcscpy(szFullFileName, szTemp);

	if ((_T('\\') == szBaseName[0] || _T('/')  == szBaseName[0] )&& 
		(_T('\\') == szDirName[_tcslen(szDirName) - 1] || _T('/') == szDirName[_tcslen(szDirName) - 1]) )
	{
		_tcscat(szFullFileName, &(szBaseName[1]));
	}
	else
	{
		_tcscat(szFullFileName, szBaseName);
	}
	return TRUE;
}

wchar_t* CFileUtil::String2WString(const char* pSource)
{
	if(NULL == pSource) 
		return NULL;

	size_t _Dsize = MultiByteToWideChar(CP_ACP, 0, pSource, -1, NULL, 0);  
	wchar_t *_Dest = new wchar_t[_Dsize]; 

	MultiByteToWideChar(CP_ACP, 0, pSource, -1, _Dest, _Dsize);  
	
	return _Dest; 
}

wstring CFileUtil::String2WString(const string& s)  
{  
	const char* _Source = s.c_str();  
	size_t _Dsize = MultiByteToWideChar(CP_ACP, 0, _Source, -1, NULL, 0);  
	wchar_t *_Dest = new wchar_t[_Dsize];  
	MultiByteToWideChar(CP_ACP, 0, _Source, -1, _Dest, _Dsize);  
	wstring result = _Dest;  
	delete []_Dest;  
	return result;  
} 

// BOOL CFileUtil::GetFileVersion(LPCTSTR szComponentPath, TCHAR* szBuffer, UINT nMaxSize)
// {
// 	if (!IsFileOrDirExist(szComponentPath))
// 			return FALSE;
// 
// 	DWORD dwBufferSize = ::GetFileVersionInfoSize(szComponentPath, NULL);
// 	if (dwBufferSize <= 0)
// 		return FALSE;
// 
// 	LPVOID lpVersionInfo = new BYTE[dwBufferSize + 1];
// 	ZeroMemory(lpVersionInfo, dwBufferSize);
// 
// 	if (GetFileVersionInfo(szComponentPath, 0, dwBufferSize, lpVersionInfo))
// 	{
// 		VS_FIXEDFILEINFO* pFixedFileInfo = NULL;
// 		UINT dwFixedInfoSize = 0;
// 
// 		BOOL bSucceed = VerQueryValue(lpVersionInfo, TEXT("\\"), (LPVOID*)&pFixedFileInfo, &dwFixedInfoSize);
// 		if (!bSucceed)
// 		{
// 			delete []lpVersionInfo;
// 			lpVersionInfo = NULL;
// 			return FALSE;
// 		}
// 
// 		WORD dwHH = HIWORD(pFixedFileInfo->dwFileVersionMS);
// 		WORD dwHL = LOWORD(pFixedFileInfo->dwFileVersionMS);
// 		WORD dwLH = HIWORD(pFixedFileInfo->dwFileVersionLS);
// 		WORD dwLL = LOWORD(pFixedFileInfo->dwFileVersionLS);
// 
// 		_stprintf(szBuffer, TEXT("%d.%d.%d.%d"), dwHH, dwHL, dwLH, dwLL);		
// 	}
// 
// 	if (NULL != lpVersionInfo)
// 	{
// 		delete []lpVersionInfo;
// 		lpVersionInfo = NULL;
// 	}
// 
// 	return TRUE;
// }

char* CFileUtil::WString2String(const wchar_t* pSource)
{
	if(NULL == pSource) return NULL;

	int nNeedLen = WideCharToMultiByte(CP_ACP, 0, 
									   pSource, -1, 
									   NULL, NULL, 
									   NULL, NULL);
	if (nNeedLen <= 0)
		return NULL;

	char* pDest = new char[nNeedLen];
	ZeroMemory(pDest, sizeof(char) * nNeedLen);
	WideCharToMultiByte(CP_ACP, 0, 
					    pSource, -1, 
						pDest, nNeedLen,
						NULL, NULL);

	return pDest;
}

BOOL CFileUtil::GetSystemDriver(LPTSTR szDriverName,UINT nMaxSize)
{
	BOOL bFind = FALSE;

	TCHAR buf[256] = _T("\0");  
	DWORD len = ::GetLogicalDriveStrings(sizeof(buf)/sizeof(TCHAR),buf);  
	for(TCHAR* s=buf; *s; s+= _tcslen(s) + 1)  
	{  
		UINT DType=::GetDriveType(s);  
		if(DType == DRIVE_FIXED)  
		{  
			_tcscpy(szDriverName,s);
			bFind = TRUE;
			break; 
		} 

	}  

	return bFind;
}
